<?php


namespace common\helper;


use Yii;
use yii\web\Response;
/**
 *
 * User: denghp
 * Date: 16-4-5
 * Time: 下午4:35
 */
class ResponseHelper extends Response
{


    /**
     * 返回成功json
     * @param $data 返回结果
     */
    public static function success($data)
    {

        $resp = \Yii::$app->response;
        $resp->isOk;
        $resp->format = Response::FORMAT_JSON;
        $result = array(
            'status' => $resp->getStatusCode(),
            'data' => $data
        );
        $resp->data = $result;
    }

    /**
     * 返回成功json
     * @param $data 返回结果
     * @param 自定义status|int $status 默认 status = 0
     * @return json
     */
    public static function formatSuccess($data, $status = 0)
    {
        $resp = \Yii::$app->response;
        $resp->isOk;
        $resp->format = Response::FORMAT_JSON;
        $result = array(
            'status' => $status,
            'data' => $data
        );
        $resp->data = $result;
    }

    public function formatXml()
    {
        $something = true;
        $return_xml = ['status' => 'error'];
        if ($something == true) {
            $return_xml = ['status' => 'success', 'message' => ' is successfully saved'];
        }
        Yii::$app->response->format = Response::FORMAT_XML;
        return $return_xml;
    }


    public static function serverError($msg)
    {

        $resp = Yii::$app->response;
        $resp->statusCode = 500;
        $resp->format = Response::FORMAT_JSON;
        $result = array(
            'status' => $resp->getStatusCode(),
            'msg' => $msg
        );
        $resp->data = $result;
    }

    /**
     * @param $status http_status
     * @param $msg
     * @return string
     */
    public static function error($status, $msg)
    {

        $resp = Yii::$app->response;
        $resp->statusCode = $status;
        $resp->format = Response::FORMAT_JSON;
        $result = array(
            'status' => $resp->getStatusCode(),
            'msg' => $msg
        );
        $resp->data = $result;
    }

    /**
     * @param $status 自定义status code
     * @param $msg
     * @return string
     */
    public static function formatError($status, $msg)
    {

        $resp = Yii::$app->response;
        $resp->statusCode = 200;
        $resp->format = Response::FORMAT_JSON;
        $result = array(
            'status' => $status,
            'msg' => $msg
        );
        $resp->data = $result;
    }


    /**
     * 获取验证失败的错误信息,返回string类型
     * @param array|错误列表 $errors 错误列表
     * @return string
     */
    public static function getErrorsAsString(array $errors)
    {
        $msg = '';
        foreach ($errors as $k => $v) {
            $msg = $msg . $v[0];
        }
        return $msg;
    }

    /**
     * 图片上传接口返回错误信息
     * @param $code
     * @param $message
     */
    public static function uploadError($code, $message)
    {
        $resp = Yii::$app->response;
        $resp->statusCode = 200;
        $resp->format = Response::FORMAT_JSON;
        $result = array(
            'error' => $code,
            'message' => $message
        );
        $resp->data = $result;
    }

    /**
     * 图片上传接口返回错误信息
     * @param $data
     * @param string $tools
     */
    public static function uploadSuccess($data, $tools = '')
    {
        $resp = Yii::$app->response;
        $resp->isOk;
        $resp->format = Response::FORMAT_JSON;
        $data['status'] = 200;
        if ($tools == 'kindeditor') {
            $data['error'] = 0;
        }
        $resp->data = $data;
    }
}